<?php

namespace App\Http\Controllers\Api;

use App\Http\Requests\UserCreateRequest;
use App\Http\Controllers\Controller;
use App\Http\Requests\UserUpdateRequest;
use App\Services\Api\ApiService;
use App\Services\PermissionService;
use App\Services\RoleService;
use App\Services\UserService;
use Carbon\Carbon;
use Illuminate\Support\Facades\Auth;
use App\User;
use Illuminate\Support\Facades\Log;

class UserController extends Controller
{
    private $service;

    public function __construct(ApiService $apiService, UserService $service)
    {
        parent::__construct($apiService);
        $this->service = $service;
    }

    /**
     * 登录验证
     * @return mixed
     */
    public function login()
    {
        if(Auth::attempt(['email' => request('email'), 'password' => request('password')]))
        {
            $user = Auth::user();
            $data =  $user->createToken('复旦预约平台')->accessToken;
            Log::info(request('email').'在'.Carbon::now().'成功登录系统');
            return $this->apiService->run(200,['data' => $user, 'token' => $data]);
        } else {
            Log::info(request('email').'在'.Carbon::now().'登录系统失败');
            return $this->apiService->run(401);
        }
    }

    /**
     * Create a new user instance after a valid registration.
     * @param UserCreateRequest $request
     * @return $this|\Illuminate\Database\Eloquent\Model
     */
    protected function create(UserCreateRequest $request)
    {
             $this->service->repository->create([
                'name' => $request->name,
                'email' => $request->email,
                'password' => bcrypt($request->password),
                 'deportment_id' => $request->deportment_id,
            ]);
            return $this->apiService->run('200');
    }

    /**
     * 获取用户信息
     * @param $id
     * @return mixed
     */
    public function show($id){
            $user = $this->service->repository->find($id);
            return $this->apiService->run(200, $user);
    }

    /**
     * 查找所有用户
     * @param $showpage
     * @return mixed
     */
    public function all(){
        $showPage = $this->getShowPage();
        if (!empty(request()->input('showpage'))) $showPage = request()->input('showpage');
        $this->service->repository->pushCriteria(app('Prettus\Repository\Criteria\RequestCriteria'));
        $users = $this->service->repository->paginate($showPage);
        return $this->apiService->run(200, $users);
    }

    /**
     * @param UserUpdateRequest $request
     * @return mixed
     */
    public function update(UserUpdateRequest $request){
            $this->service->repository->find(Auth::user()->id)->update([
                'name' => $request->name,
                'email' => $request->email,
                'password' => bcrypt($request->password),
                'deportment_id' => $request->deportment_id,
            ]);
            return $this->apiService->run('200');
    }


   /**
     * @param $id
     * @return mixed
     */
     public function destroy($id){
        $this->service->repository->destroy($id);
        return $this->apiService->run(200);
    }

    /**
     * 用户绑定角色
     * @param $id
     * @param $role_id
     * @param RoleService $roleService
     * @return mixed
     */
    public function userBindRole($id, $role_id, RoleService $roleService){
         $user = $this->service->repository->find($id)->attachRole($roleService->repository->find($role_id));
         return $this->apiService->run(200, $user);
    }

    /**
     * 用户移除一个角色
     * @param $id
     * @param $role_id
     * @param RoleService $roleService
     * @return mixed
     */
    public function userRemoveRole($id, $role_id, RoleService $roleService){
        $user = $this->service->repository->find($id)->detachRole($roleService->repository->find($role_id));
        return $this->apiService->run(200, $user);
    }

    /**
     * 用户移除所有角色
     * @param $id
     * @return mixed
     */
    public function userRemoveAllRole($id){
        $user = $this->service->repository->find($id)->detachAllRoles();
        return $this->apiService->run(200, $user);
    }

    /**
     * 用户绑定权限
     * @param $id
     * @param $permission_id
     * @param PermissionService $permissionService
     * @return mixed
     */
    public function userBindPermission($id, $permission_id, PermissionService $permissionService){
        $user = $this->service->repository->find($id)->attachPermission($permissionService->repository->find($permission_id));
        return $this->apiService->run(200, $user);
    }

    /**
     * 移除角色一个权限
     * @param $id
     * @param $permission_id
     * @param PermissionService $permissionService
     * @return mixed
     */
    public function userRemovePermission($id, $permission_id, PermissionService $permissionService){
        $user = $this->service->repository->find($id)->detachPermission($permissionService->repository->find($permission_id));
        return $this->apiService->run(200, $user);
    }

    /**
     * 移除所有权限
     * @param $id
     * @return mixed
     */
    public function userRemoveAllPermission($id){
        $user = $this->service->repository->find($id)->detachAllPermissions();
        return $this->apiService->run(200, $user);
    }

}
